package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.RedisConstant;

import com.itheima.dao.UserManagerDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;

import com.itheima.pojo.UserManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service(interfaceClass = UserManagerService.class)
@Transactional
public class UserManagerServiceimpl implements UserManagerService {

        @Autowired
        private UserManagerDao userManagerDao;
        @Autowired
        private JedisPool jedisPool;
        /**
         * 添加套餐基本信息和中间表
         * @param roleIds
         * @param userManager
         */
        @Override
        public void add(Integer[] roleIds, UserManager userManager) {
            userManagerDao.addDataBaseSetMeal(userManager);
            this.addSetMealAndCheckGroud(roleIds,userManager.getId());
//          把数据图的reids放入
          //  jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        }


        /**
         * 插入中间表
         * @param checkgroupIds
         * @param setMealId
         */
        private void addSetMealAndCheckGroud(Integer[] checkgroupIds,Integer setMealId) {
            for (Integer checkgroupId: checkgroupIds){
                HashMap<String, Object> map = new HashMap<>();
                map.put("checkgroupId", checkgroupId);
                map.put("setMealId", setMealId);
                userManagerDao.addSetMealAndCheckGroud(map);
            }
        }

        /**
         * 分页查询套餐数据
         * @return
         */
        @Override
        public PageResult findAllSetMeal(QueryPageBean queryPageBean) {

            PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
            String queryString=queryPageBean.getQueryString();
            if (queryString==null){
                queryString="";
            }
            Page<UserManager> page=userManagerDao.findByCondition("%"+queryString+"%");
            return  new PageResult(page.getTotal(),page.getResult());
        }

        /**
         * 根据id--回显套餐基本信息
         * @param id
         * @return
         */
        @Override
        public UserManager findSetMealById(Integer id) {
            UserManager manager = userManagerDao.findSetMealById(id);
            return manager;
        }

        /**
         * 根据套餐id查看--勾选项目
         * @param id
         * @return
         */
        @Override
        public Integer[] findGroups(Integer id) {
            return userManagerDao.findGroups(id);
        }

        /**
         * 更改套餐
         * @param roleIds
         * @param userManager
         */
        @Override
        public void edit(Integer[] roleIds, UserManager userManager) {
//         1.更新套餐基本信息表
            userManagerDao.updateBaseSetMeal(userManager);
            Integer URid= userManagerDao.findUserAndRoleById(userManager.getId());
            if(URid>0){
               // 2. 删除中间表
                userManagerDao.deleteSetMealAndCheckGroup(userManager.getId());
            }
//
//         3.插入中间表
            for (Integer roleId:roleIds){
                HashMap<String, Object> map = new HashMap<>();
                map.put("roleId", roleId);
                map.put("userManagerId", userManager.getId());
                userManagerDao.addSetMealAndCheckGroup(map);
            }
        }

        /**
         * 查找全部套餐数据
         * @return
         */
        @Override
        public List<UserManager> getSetmeal() {
            return userManagerDao.getSetmeal();
        }
        /*
        删除
         */

    @Override
    public void delete(Integer id) {
        userManagerDao.delete(id);
       // 2. 删除中间表
        userManagerDao.deleteSetMealAndCheckGroup(id);
    }
    /**
         * 寻找套餐名字和套餐预约人数
         * @return
         */
//        @Override
//        public List<Map<String, Object>> findOrderCountSetmeal() {
//            return userManagerDao.findOrderCountSetmeal();
//        }
    }


